package org.lewis.common.store;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.commons.lang.StringUtils;

import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.OSSException;
import com.aliyun.openservices.oss.model.GetObjectRequest;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.aliyun.openservices.oss.model.PutObjectResult;

public class OSSUtils {

	private  static OSSClient client;
	private final static String accessId = "cIirUjPDNBE9Cfm7";
	private final static String accessKey = "G9sBK9lpqSBDgf4L7DuVRbC7OxyH62";
	private final static String ossEndpoint = "http://oss.aliyuncs.com/";
	private final static String mp3bucketName = "lewism1";
	private final static String photobucketName = "lewism2";
	
	static {
		client = new OSSClient(ossEndpoint, accessId, accessKey);
	}

	/**
	 * 存储用户头像
	 * 
	 * @param image
	 * @param objectName
	 * @param uid
	 * @return
	 * @throws FileNotFoundException
	 * @throws OSSException
	 * @throws ClientException
	 */
	public static PutObjectResult storeUserFace(File image, String objectName,
			Long uid) throws FileNotFoundException, OSSException,
			ClientException {
		ObjectMetadata objectMeta = new ObjectMetadata();
		objectMeta.addUserMetadata("uid", String.valueOf(uid));
		objectMeta.addUserMetadata("group", "userface");
		objectMeta.setContentLength(image.length());
		objectMeta.setContentType("image/jpeg");
		return client.putObject(photobucketName, objectName,new FileInputStream(image), objectMeta);
	}

	/**
	 * 获取用户头像
	 * 
	 * @param objectName
	 * @param downloadFile
	 * @throws OSSException
	 * @throws ClientException
	 */
	public static void getUserFace(String objectName, File downloadFile)
			throws OSSException, ClientException {
		client.getObject(new GetObjectRequest(photobucketName, objectName),downloadFile);
	}

	/**
	 * 删除用户头像
	 * 
	 * @param objectName
	 * @throws OSSException
	 * @throws ClientException
	 */
	public static void deleteUserFace(String objectName) throws OSSException,
			ClientException {
		client.deleteObject(photobucketName, objectName);
	}

	/**
	 * 存储歌曲
	 * 
	 * @param song
	 * @param objectName
	 * @param uid
	 * @return
	 * @throws FileNotFoundException
	 * @throws OSSException
	 * @throws ClientException
	 * @throws FileNotFoundException 
	 */
	public static PutObjectResult storeSong(File song,
			String objectName, Long uid) throws OSSException, ClientException, FileNotFoundException {
		ObjectMetadata objectMeta = new ObjectMetadata();
		objectMeta.addUserMetadata("uid", String.valueOf(uid));
		objectMeta.addUserMetadata("group", "song");
		objectMeta.addUserMetadata("time", String.valueOf(System.currentTimeMillis()));
		objectMeta.setContentLength(song.length());
		return client.putObject(mp3bucketName, objectName,new FileInputStream(song), objectMeta);
	}
	
	/**
	 * 存储通用图片
	 * 
	 * @param image
	 * @param objectName
	 * @param uid
	 * @return
	 * @throws FileNotFoundException
	 * @throws OSSException
	 * @throws ClientException
	 */
	public static PutObjectResult storeCommonPic(File image, String objectName,String tag) throws FileNotFoundException, OSSException,
			ClientException {
		if(StringUtils.isBlank(tag)){
			throw new RuntimeException("请指定图片上传模块名");
		}
		ObjectMetadata objectMeta = new ObjectMetadata();
		objectMeta.addUserMetadata("tag", tag);
		objectMeta.setContentLength(image.length());
		return client.putObject(tag, objectName,new FileInputStream(image), objectMeta);
	}


}
